package com.ruoyi.smart.XxlJon;

import com.mongodb.client.result.DeleteResult;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class SmartJob {

    @Autowired
    MongoTemplate mongoTemplate;

    @XxlJob("demoJob")
    @Async("taskExecutor")
    public void demoJob(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -3);

        Date time = calendar.getTime();

        String format = sdf.format(time);

        Query query = new Query(Criteria.where("lastDate").lt(format));

        DeleteResult messageDb = mongoTemplate.remove(query, "messageDb");
        System.out.println(format+"之前的聊天记录已删除");

    }
}
